package com.wohuijituan.whcsmerchant.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wohuijituan.whcsmerchant.R;
import com.wohuijituan.whcsmerchant.activity.CardManageActivity;
import com.wohuijituan.whcsmerchant.activity.CostSettlementActivity;
import com.wohuijituan.whcsmerchant.activity.NoticeRecordActivity;
import com.wohuijituan.whcsmerchant.activity.WebViewActivity;
import com.wohuijituan.whcsmerchant.bean.SellMessageBean;
import com.wohuijituan.whcsmerchant.callback.SellMessageCallBack;
import com.wohuijituan.whcsmerchant.url.ParamsKey;
import com.wohuijituan.whcsmerchant.url.Urls;
import com.wohuijituan.whcsmerchant.utils.LogUtils;
import com.wohuijituan.whcsmerchant.utils.MyOkHttpUtils;
import com.wohuijituan.whcsmerchant.utils.MyRequestParams;

import okhttp3.Call;

/**
 * A simple {@link Fragment} subclass.
 */
public class ShopFragment extends MyBaseFragment implements View.OnClickListener{

    private static final String TAG = "ShopFragment";
    public ShopFragment() {
        // Required empty public constructor
    }

    private TextView tvOrderToday;
    private TextView tvSaleToday;
    private TextView tvSaleAll;
    private LinearLayout llCostSettlement;
    private LinearLayout llOrderForm;
    private LinearLayout llReleaseNotice;
    private LinearLayout llSystemNotice;
    private LinearLayout llCommonQuestion;
    private LinearLayout llOperateLog;
    private LinearLayout llBankCard;
    private ImageButton titleGoBack;
    private TextView titleName;
    private void assignViews(View view) {
        tvOrderToday = (TextView) view.findViewById(R.id.tv_order_today);
        tvSaleToday = (TextView) view.findViewById(R.id.tv_sale_today);
        tvSaleAll = (TextView) view.findViewById(R.id.tv_sale_all);
        llCostSettlement = (LinearLayout) view.findViewById(R.id.ll_cost_settlement);
        llOrderForm = (LinearLayout) view.findViewById(R.id.ll_order_form);
        llReleaseNotice = (LinearLayout) view.findViewById(R.id.ll_release_notice);
        llSystemNotice = (LinearLayout) view.findViewById(R.id.ll_system_notice);
        llCommonQuestion = (LinearLayout) view.findViewById(R.id.ll_common_question);
        llOperateLog = (LinearLayout) view.findViewById(R.id.ll_operate_log);
        llBankCard = (LinearLayout) view.findViewById(R.id.ll_bank_card);
        titleGoBack = (ImageButton) view.findViewById(R.id.title_go_back);
        titleName = (TextView) view.findViewById(R.id.title_name);
    }


    private MyOkHttpUtils myOkHttpUtils;
    private MyRequestParams params;

    @Override
    public View createMyView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_shop, container, false);
        assignViews(view);
        initData();
        initView();
        initListener();
        sellMessageRequest();
        return view;
    }

    private void initData() {
        myOkHttpUtils = new MyOkHttpUtils();
        params = new MyRequestParams();
    }

    private void initListener() {
        llCostSettlement.setOnClickListener(this);
        llOrderForm.setOnClickListener(this);
        llReleaseNotice.setOnClickListener(this);
        llSystemNotice.setOnClickListener(this);
        llCommonQuestion.setOnClickListener(this);
        llOperateLog.setOnClickListener(this);
        llBankCard.setOnClickListener(this);


    }

    private void initView() {
        titleGoBack.setVisibility(View.GONE);
        titleName.setText(R.string.shop_manage);

    }

    /**
     * 请求今日订单,售额等信息展示
     */
    private void sellMessageRequest(){
        showDialog(ctx);
        String strUrl = Urls.SellMessage;
        params.clear();
        params.addStringRequest(ParamsKey.UserKey, myApplication.getUserKey());
        myOkHttpUtils.postRequest(strUrl, params, new SellMessageCallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                dismissDialog();
                showToast(ctx, "网络出错,请检查网络是否连接!");
                LogUtils.e(TAG, e.getMessage());
            }

            @Override
            public void onResponse(SellMessageBean response, int id) {
                if (response.getState() == 1) {
                    Message msg = new Message();
                    msg.what = SellMessageRequest_OK;
                    msg.obj = response;
                    handler.sendMessage(msg);
                } else {
                    showToast(ctx, response.getMsg());
                    dismissDialog();
                }
            }
        });
    }

    /**
     * 填充今日订单,今日售额,总售额等信息
     * @param data
     */
    private void fillHeadView(SellMessageBean.DataEntity data){
        tvOrderToday.setText(data.getOrderCount());
        tvSaleAll.setText(data.getAllMoney());
        tvSaleToday.setText(data.getTodayMoney());
    }
    private static final int SellMessageRequest_OK = 5695;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case SellMessageRequest_OK:
                    SellMessageBean messageBean = (SellMessageBean) msg.obj;
                    if (messageBean.getData()!=null){
                        fillHeadView(messageBean.getData());
                    }

                    dismissDialog();
                    break;
            }
        }
    };
    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()){
            case R.id.ll_cost_settlement:
                //TODO 费用结算
                intent.setClass(activity,CostSettlementActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_order_form:
                intent.setClass(activity,WebViewActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_release_notice:
                // 发布公告
                intent.setClass(activity,NoticeRecordActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_system_notice:
                //TODO 系统公告
                intent.setClass(activity,WebViewActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_common_question:
                //TODO 常见问题
                intent.setClass(activity,WebViewActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_operate_log:
                //TODO 操作日志
                intent.setClass(activity,WebViewActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_bank_card:
                //TODO 我的银行
                intent.setClass(activity,CardManageActivity.class);
                startActivity(intent);
                break;
        }
    }
}
